package com.yz.lzq.liwushuo.ui.modules.category.danping;

import com.yz.lzq.liwushuo.config.UrlConfig;
import com.yz.lzq.liwushuo.jsonbean.category.CategroyDanPBean;
import com.yz.lzq.liwushuo.ui.modules.category.CategorInterface;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by AdminJia on 2016/11/28.
 */

public class CategoryDanPBiz implements ICategoryDanPBiz {
    @Override
    public void getCategoryData(final OnCategoryDanPListener listener) {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConfig.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()).build();
        CategorInterface.CategoryDanPInterface categoryDanPInterface = retrofit.create(CategorInterface.CategoryDanPInterface.class);
        Call<CategroyDanPBean> call = categoryDanPInterface.getData();
        call.enqueue(new Callback<CategroyDanPBean>() {
            @Override
            public void onResponse(Call<CategroyDanPBean> call, Response<CategroyDanPBean>
                    response) {
                List<Object> list_object = new ArrayList<>();
                List<CategroyDanPBean.DataBean.CategoriesBean> categories = response.body()
                        .getData().getCategories();
                for (int i = 0; i < categories.size(); i++) {
                    list_object.add(categories.get(i));
                    list_object.addAll(categories.get(i).getSubcategories());

                }
                listener.getDataSuccess(list_object);
                listener.getListViewDataSuccess(categories);

            }

            @Override
            public void onFailure(Call<CategroyDanPBean> call, Throwable t) {
                listener.getDataFailed(t);
            }
        });

    }


}
